$(function () {
    $("#jqGrid").jqGrid({
        url: '../sysjob/list',
        datatype: "json",
        colModel: [			
			{ label: 'jobId', name: 'jobId', index: 'JOB_ID', width: 50, key: true },
			{ label: '调度器名称', name: 'jobName', index: 'JOB_NAME', width: 80 }, 	
			{ label: '分组', name: 'jobGroup', index: 'JOB_GROUP', width: 80 }, 	
			{ label: '状态', name: 'jobStatus', index: 'JOB_STATUS', width: 80 }, 	
			{ label: '表达式', name: 'cronExpression', index: 'CRON_EXPRESSION', width: 80 }, 	
			{ label: '类的路径', name: 'beanClass', index: 'BEAN_CLASS', width: 80 }, 	
			{ label: '描述', name: 'description', index: 'DESCRIPTION', width: 80 }, 	
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		sysJob: {},
		q:{
			jobName: null , 
			jobGroup: null , 
			jobStatus: null , 
			cronExpression: null , 
			beanClass: null , 
			description: null , 
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		reset: function() {
			$('#sysJobQuery :input').each(function(){
					$(this).val();
			});
			$('#sysJobQuery').find("select").each(function(){
					$(this).val();
			});						
			vm.q.jobName = null;

			vm.q.jobGroup = null;

			vm.q.jobStatus = null;

			vm.q.cronExpression = null;

			vm.q.beanClass = null;

			vm.q.description = null;

			vm.reload();
		},		
		add: function(){
			vm.sysJob = {};
			createDialog("新增","winContent","jqGrid","800px","600px");
		},
		update: function (event) {
			var jobId = getSelectedRow();
			if(jobId == null){
				return ;
			}
            
            updateDialog("修改","winContent","jqGrid","800px","600px");
            vm.getInfo(jobId)
		},
		push:function(event){
			var jobIds = getSelectedRows();
			if(jobIds == null){
				return ;
			}
			var rowData = $("#jqGrid").jqGrid('getRowData',jobIds);
			var dataStr = JSON.stringify(rowData);
			confirm('确定要发布调度作业到注册中心?', function(){
				$.ajax({
					type: "POST",
				    url: "../sysjob/push",
				    data: rowData,
				    success: function(r){
						if(r.code == 0){
							alert('操作成功');
						}else{
							alert(r.msg);
						}
					}
				});
			});			
		},
		saveOrUpdate: function (event) {
			var formConfig = {
					id : 'sysJobForm',
					url : vm.sysJob.jobId == null ? "../sysjob/save" : "../sysjob/update",
					callback : function(r){
				    	layer.closeAll();
				    	if(r.code === 0){
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				};
			
			var form = valForm(formConfig);
			form.ajaxPost(false);
		},
		del: function (event) {
			var jobIds = getSelectedRows();
			if(jobIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../sysjob/delete",
				    contentType: "application/json",
				    data: JSON.stringify(jobIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(jobId){
			$.get("../sysjob/info/"+jobId, function(r){
                vm.sysJob = r.sysJob;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:vm.q,
                page:page
            }).trigger("reloadGrid");
		}
	}
});